﻿#ifndef MSGSOCKET_H
#define MSGSOCKET_H

#include <QThread>
#include <QTcpSocket>
#include <QDataStream>
#include "globalvars.h"

class MsgSocket : public QThread
{
    Q_OBJECT
signals:
    void signalErrorInterNet();
    void signalSendMsg(const QString& msg);
    void signalSendImage(const QString& msg,const QString& path);

    void signalUserLoginResult(bool result, const QString& msg);
    void signalUserLogoutResult(bool result);
    void signalUserRegisterResult(bool result,const QString& msg);
    void signalGainUserInfoResult(bool result, const QString& msg);
    void signalModifyPasswordResult(bool result,const QString& msg);
    void signalModifyUserInfosResult(bool result,const QString& msg);
    void signalUploadCertificateResult(bool result,const QString& msg);
    void signalUploadCertImageSchedule();

    ///hotel
    void signalSetHotelInfoList(bool result,const QString& msg);
    void signalSetHotelDetails(bool result,const QString& msg);

    ///house
    void signalSetHouseList(bool result,const QString& msg);
    void signalSetHouseImage();

    ///reserve
    void signalSubmitReserveResult(bool result,const QString& msg);
    void signalSetReserveList(bool result,const QString& state,const QString& commentState);
    void signalSetReserveDetails(bool result,const QString& msg);
    void signalReserveDeleteResult(bool result,const QString& msg);
    void signalSetReserveHotelImage(bool result, const QString& reserveID, const QImage& image);

    ///comment
    void signalSubmitCommentResult(bool result,const QString& msg);
    void signalSetCommentList(bool result,const QString& msg);
    void signalSetCommentImage(bool result, const QString& commentID, const QImage& image);

    ///save
    void signalSetSaveList(bool result,const QString& msg);
    void signalSubmitSaveResult(bool result,const QString& msg);
    void signalSetSaveHotelImage(bool result, const QString& saveID, const QImage& image);

    ///download image
    void signalUserImageResult(bool result, const QImage& image);
    void signalHotelSearchImage(const QString& hotelID, const QImage& image);
    void signalHotelDetailsImage();
    void signalHouseImageResult(const QString& type, const QImage& image);

    //////////////******* business ******//////////////
    void signalSetHotelManageImage(bool result, const QString& hotelID, const QImage& image);
    void signalHotelImageForBus();
    void signalSetHouseListForBus(bool result,const QString& msg);
    void signalHouseImageResultForBus(const QString& type, const QImage& image);
    void signalSubmitHouseResult(bool result,const QString& msg);
    void signalDeleteHouseResult(bool result,const QString& msg);
    void signalSubmitHotelResult(bool result,const QString& msg);
    void signalDeleteHotelResult(bool result,const QString& msg);

    void signalSetHotelManageList(bool result,const QString& msg);
    void signalUpdateOrderManageShow();
    void signalModifyOrderStateResult(bool result,const QString& msg);

    void signalSetCommentListForBus(bool result,const QString& msg);
    void signalSubmitBusCommentResult(bool result,const QString& msg);
    void signalSetCommentImageForBus(bool result, const QString& commentID, const QImage& image);


    //////////////******* worker ******//////////////
    void signalUpdateOrderShowForWorker();
    void signalSetHotelCertList();
    void signalHotelImageForWorker();
    void signalHotelCertResult(bool result,const QString& msg);
    void signalSetUserCertList(bool result,const QString& msg);
    void signalUserCertResult(bool result,const QString& msg);
    void signalSetUserCertImage(bool result, const QString& userID, const QImage& image);

public slots:
    void slotReadyRead();
    void slotSendMsg(const QString&  msg);
    void slotSendImage(const QString& msg,const QString& path);
    void slotSendImage(const QString& msg, const QStringList& pathList);

    void slotGainHouseList(QString index, QString hotelID = GlobalVars::g_localHotel.getHotel());
    void slotGainHotelImage(QString index, QString hotelID = GlobalVars::g_localHotel.getHotel());
    void slotGainHouseImage();
    void slotGainOrederFormList();
    void slotGainHotelCommentList(QString index, QString hotelID = GlobalVars::g_localHotel.getHotel());

public:
    explicit MsgSocket(QThread *parent = nullptr);
    ~MsgSocket();

    void exitThread(void);

protected:
    void run();

private:
    int m_imageIndex;
    int m_imageCount;
    QStringList m_imagePathList;

    bool m_isExit;
    quint16 m_tcpBlockSize;
    QTcpSocket *m_tcpSocket;

    void parseUserAsk(const ImageMsgMap& imageMap);
    void parseUseImage(QString data, const QImage &image);

    void sendMoreImage(const QString& msg);
    void parseUserAsk(QString msg);
    void parseUserExit(QString data);
    void parseUserLogin(QString data);
    void parseUserRegister(QString data);
    void parseUserInfo(QString data);
    void parseModifyPasswordResult(QString data);
    void parseModifyUserInfosResult(QString data);
    void parseUploadHeadPicResult(QString data);
    void parseUploadCertificateResult(QString data);


    /// hotel Infos
    void parseHotelInfoList(QString data);
    void parseHotelImageSearch(QString data,const QImage &image);
    void setHotelDetails(const QStringList& list);
    void parseHotelDetailsImage(QString data,const QImage &image);

    /// house Infos
    void parseGetHouseList(QString data);
    void parseHouseImage(QString data,const QImage &image);
    void parseHotelCredImage(QString data, const QImage &image);

    /// reserve Infos
    void parseSubmitReserveResult(QString data);
    void parseGetReserveList(QString data);
    void parseReserveDelete(QString data);
    void parseGetReserveDetails(QString data);
    void parseReserveHotelImage(QString data, const QImage &image);

    /// comment Infos
    void parseSubmitCommentResult(QString data);
    void parseGainComment(QString data);
    void parseSubmitCommentImageResult(QString data);
    void parseCommentImage(QString data, const QImage &image);

    /// save Infos
    void parseGetSaveList(QString data);
    void parseSubmitSaveResult(QString data);
    void parseSaveHotelImage(QString data, const QImage &image);
    void parseHotelDetails(const QString &data);

    ////////////////////////////////////// business ////////////////////////////////////
    void parseHotelManageIdAndName(const QString &data);
    void parseHotelManageList(const QString &data);
    void parseHotelManageImage(QString data, const QImage &image);
    void parseSubmitHouseResult(const QString& data);
    void parseSubmitHouseImageResult(const QString& data);
    void parseDeleteHouseResult(const QString& data);
    void parseSubmitHotelResult(const QString& data);
    void parseSubmitHotelImageResult(const QString& data);
    void parseDeleteHotelResult(const QString& data);

    void parseOrderManageList(const QString &data);
    void parseModifyOrderState(const QString &data);
    void parseSubmitBusCommentResult(const QString& data);

    ////////////////////////////////////// worker ////////////////////////////////////
    void parseHotelCertResult(const QString &data);
    void parseUserCertList(const QString &data);
    void parseUserCertResult(const QString &data);
    void parseUserCertImage(QString data, const QImage &image);
};

#endif // MSGSOCKET_H
